/*

* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Smart-Shop开源商城系统/ Java商城-首选电商平台系统 请务必保留此注释头信息
* |     开源地址: https://gitee.com/ningbo-qishan/gruul-mall
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     可正常分享和学习源码,未经授权 不可商用！
* |     商业版中台购买/开源版商业授权 联系技术客服
* |     官网:  https://www.73app.cn/
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Copyright (c) 2016-2999 宁波启山智软科技有限公司
* |     版权所有 ,侵权必究！
* ------------------------------------------------------------------------

*/
package com.medusa.gruul.solitaire.service.api.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.additional.update.impl.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.medusa.gruul.common.core.constant.CommonConstants;
import com.medusa.gruul.common.core.exception.ServiceException;
import com.medusa.gruul.common.core.util.CurUserUtil;
import com.medusa.gruul.common.core.util.Result;
import com.medusa.gruul.common.core.util.SystemCode;
import com.medusa.gruul.common.dto.CurUserDto;
import com.medusa.gruul.goods.api.feign.RemoteGoodsService;
import com.medusa.gruul.goods.api.model.vo.api.ApiProductVo;
import com.medusa.gruul.goods.api.model.vo.api.ApiSkuStockVo;
import com.medusa.gruul.order.api.model.OrderItemVo;
import com.medusa.gruul.order.api.model.OrderVo;
import com.medusa.gruul.solitaire.api.entity.SolitaireActivity;
import com.medusa.gruul.solitaire.api.entity.SolitaireRecord;
import com.medusa.gruul.solitaire.api.model.vo.api.ApiSolitaireActivityProductVo;
import com.medusa.gruul.solitaire.api.model.vo.api.ApiSolitaireActivityVo;
import com.medusa.gruul.solitaire.api.model.vo.api.ApiSolitaireRecordVo;
import com.medusa.gruul.solitaire.mapper.api.ApiSolitaireActivityMapper;
import com.medusa.gruul.solitaire.mapper.api.ApiSolitaireRecordMapper;
import com.medusa.gruul.solitaire.service.api.IApiSolitaireActivityService;
import com.medusa.gruul.solitaire.web.enums.ActivityProductTypeEnum;
import com.medusa.gruul.solitaire.web.enums.ActivityStatusEnum;
import com.medusa.gruul.solitaire.web.utils.SortUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;


/**
 * <p>
 * 接龙活动信息 服务实现类
 * </p>
 *
 * @author lcysike
 * @since 2020-06-05
 */
@Slf4j
@Service
public class ApiSolitaireActivityServiceImpl extends ServiceImpl<ApiSolitaireActivityMapper, SolitaireActivity> implements IApiSolitaireActivityService {

	@Resource
	private RemoteGoodsService remoteGoodsService;

	@Autowired
	private ApiSolitaireRecordMapper apiSolitaireRecordMapper;

	/**
	 * 获取接龙活动信息明细
	 *
	 * @return 接龙活动信息对象
	 */
	@Override
	public Result<ApiSolitaireActivityVo> getCurrentActivity() {
		ApiSolitaireActivityVo apiSolitaireActivityVo;
		//获取所有的接龙活动信息
		List<ApiSolitaireActivityVo> apiSolitaireActivityVos = this.baseMapper.queryCurrentActivity();
		if (CollectionUtil.isNotEmpty(apiSolitaireActivityVos)) {
			//先判断有没有进行中的活动 有的话直接返回  没有的话取最近一场未开始的活动 未开始的也没有则取最近一场已结束的活动
			List<ApiSolitaireActivityVo> apiSolitaireActivityVoList = apiSolitaireActivityVos.stream()
					.filter(bean -> CommonConstants.NUMBER_ONE.equals(bean.getStatus())).collect(Collectors.toList());
			if (CollectionUtil.isNotEmpty(apiSolitaireActivityVoList)) {
				//取进行中的活动的信息
				apiSolitaireActivityVo = apiSolitaireActivityVoList.get(CommonConstants.NUMBER_ZERO);
			} else {
				//查询是否有未开始的的活动
				List<ApiSolitaireActivityVo> notBeginList = apiSolitaireActivityVos.stream()
						.filter(bean -> CommonConstants.NUMBER_ZERO.equals(bean.getStatus())).collect(Collectors.toList());
				if(CollectionUtil.isNotEmpty(notBeginList)){
					//取未开始的活动的信息
					apiSolitaireActivityVo = notBeginList.get(CommonConstants.NUMBER_ZERO);
				}else{
					//说明只有已结束活动 按结束时间降序显示
					Integer lastCount = apiSolitaireActivityVos.size() - CommonConstants.NUMBER_ONE;
					apiSolitaireActivityVo = apiSolitaireActivityVos.get(lastCount);
				}
			}
			if (BeanUtil.isEmpty(apiSolitaireActivityVo)) {
				throw new ServiceException("活动信息不存在！", SystemCode.DATA_EXISTED.getCode());
			}
			//查询接龙活动的购买记录
			List<ApiSolitaireRecordVo> apiSolitaireRecordVos = apiSolitaireRecordMapper
					.getSolitaireRecord(apiSolitaireActivityVo.getId());
			if (CollectionUtil.isNotEmpty(apiSolitaireRecordVos)) {
				//去重获取接龙活动购买人数
				List<ApiSolitaireRecordVo> orderDistinct = apiSolitaireRecordVos.stream()
						.filter(SortUtil.distinctByKey(ApiSolitaireRecordVo::getOrderId)).collect(Collectors.toList());
				List<ApiSolitaireRecordVo> userDistinct = apiSolitaireRecordVos.stream()
						.filter(SortUtil.distinctByKey(ApiSolitaireRecordVo::getBuyerId)).collect(Collectors.toList());
				apiSolitaireActivityVo.setUserRecords(userDistinct.size());
				apiSolitaireActivityVo.setOrderRecords(orderDistinct.size());
				List<ApiSolitaireRecordVo> solitaireRecordVos = new ArrayList<>(CommonConstants.NUMBER_FOUR);
				for (int i = 0; i < apiSolitaireRecordVos.size(); i++) {
					//获取前四条接龙记录
					if (i < 4) {
						solitaireRecordVos.add(apiSolitaireRecordVos.get(i));
					}
				}
				apiSolitaireActivityVo.setSolitaireRecords(solitaireRecordVos);
			} else {
				apiSolitaireActivityVo.setUserRecords(CommonConstants.NUMBER_ZERO);
				apiSolitaireActivityVo.setOrderRecords(CommonConstants.NUMBER_ZERO);
			}
			return Result.ok(apiSolitaireActivityVo);
		} else {
			throw new ServiceException("暂无接龙活动!", SystemCode.DATA_EXISTED.getCode());
		}
	}

	/**
	 * 获取单场接龙活动详细信息
	 *
	 * @param activityId
	 * @return 接龙活动信息对象
	 */
	@Override
	public Result<ApiSolitaireActivityVo> getCurrentActivityById(Long activityId) {
		CurUserDto curUserDto = CurUserUtil.getHttpCurUser();
		String userId = curUserDto.getUserId();
		ApiSolitaireActivityVo apiSolitaireActivityVo = this.baseMapper.queryActivityById(activityId);
		if (!BeanUtil.isEmpty(apiSolitaireActivityVo)) {
			//调用商品服务获取接龙活动的商品信息
//			List<ApiSolitaireActivityProductVo> apiSolitaireActivityProductVos = apiSolitaireActivityVo
//					.getApiSolitaireActivityProductVos();
//			List<Long> productIds = new ArrayList<>(apiSolitaireActivityProductVos.size());
//			if (CollectionUtil.isNotEmpty(apiSolitaireActivityProductVos)) {
//				productIds = apiSolitaireActivityProductVos.stream().map(ApiSolitaireActivityProductVo::getProductId)
//						.collect(Collectors.toList());
//			}
//			List<ApiProductVo> productVos = remoteGoodsService
//					.getSolitaireActivityProduct(apiSolitaireActivityVo.getProductType(), productIds);
//			apiSolitaireActivityVo.setApiProductVos(productVos);
			//查询接龙活动的购买记录
			List<ApiSolitaireRecordVo> apiSolitaireRecordVos = apiSolitaireRecordMapper
					.getSolitaireRecord(apiSolitaireActivityVo.getId());
			if (CollectionUtil.isNotEmpty(apiSolitaireRecordVos)) {
				//去重获取接龙活动购买人数
				List<ApiSolitaireRecordVo> orderDistinct = apiSolitaireRecordVos.stream()
						.filter(SortUtil.distinctByKey(ApiSolitaireRecordVo::getOrderId)).collect(Collectors.toList());
				List<ApiSolitaireRecordVo> userDistinct = apiSolitaireRecordVos.stream()
						.filter(SortUtil.distinctByKey(ApiSolitaireRecordVo::getBuyerId)).collect(Collectors.toList());
				apiSolitaireActivityVo.setUserRecords(userDistinct.size());
				apiSolitaireActivityVo.setOrderRecords(orderDistinct.size());
				List<ApiSolitaireRecordVo> solitaireRecordVos = new ArrayList<>(CommonConstants.NUMBER_FOUR);
				List<ApiSolitaireRecordVo> selfSolitaireRecordVos = new ArrayList<>(apiSolitaireRecordVos.size());
				for (int i = 0; i < apiSolitaireRecordVos.size(); i++) {
					//获取前四条接龙记录
					if (i < 4) {
						solitaireRecordVos.add(apiSolitaireRecordVos.get(i));
					}
					//用户自己的购买记录
					if (userId.equals(apiSolitaireRecordVos.get(i).getBuyerId())) {
						selfSolitaireRecordVos.add(apiSolitaireRecordVos.get(i));
					}
				}
				apiSolitaireActivityVo.setSelfSolitaireRecords(selfSolitaireRecordVos);
				apiSolitaireActivityVo.setSolitaireRecords(solitaireRecordVos);
			} else {
				apiSolitaireActivityVo.setUserRecords(CommonConstants.NUMBER_ZERO);
				apiSolitaireActivityVo.setOrderRecords(CommonConstants.NUMBER_ZERO);
			}
			//接龙活动浏览次数累加 同时数据库更新该场活动的浏览次数
			apiSolitaireActivityVo.setViews(apiSolitaireActivityVo.getViews() + CommonConstants.NUMBER_ONE);
			boolean sign = new LambdaUpdateChainWrapper<>(this.baseMapper)
					.eq(SolitaireActivity::getId, apiSolitaireActivityVo.getId())
					.set(SolitaireActivity::getViews, apiSolitaireActivityVo.getViews()).update();
			if (!sign) {
				throw new ServiceException("浏览次数更新失败！", SystemCode.DATA_UPDATE_FAILED.getCode());
			}
			return Result.ok(apiSolitaireActivityVo);
		} else {
			throw new ServiceException("暂无接龙活动!", SystemCode.DATA_EXISTED.getCode());
		}
	}

	/**
	 * 转发当前接龙活动商品信息
	 *
	 * @param activityId
	 * @param path
	 * @return 接龙活动信息对象
	 */
	@Override
	public Result getCurrentActivityProduct(Long activityId, String path) {
		//拼接接龙信息
		ApiSolitaireActivityVo apiSolitaireActivityVo = this.baseMapper.queryActivityById(activityId);
		if (BeanUtil.isEmpty(apiSolitaireActivityVo)) {
			throw new ServiceException("接龙活动不存在", SystemCode.DATA_EXISTED.getCode());
		}
		//调用商品服务获取接龙活动的商品信息
		List<ApiSolitaireActivityProductVo> apiSolitaireActivityProductVos = apiSolitaireActivityVo
				.getApiSolitaireActivityProductVos();
		List<Long> productIds = new ArrayList<>(apiSolitaireActivityProductVos.size());
		if (CollectionUtil.isNotEmpty(apiSolitaireActivityProductVos)) {
			productIds = apiSolitaireActivityProductVos.stream().map(ApiSolitaireActivityProductVo::getProductId)
					.collect(Collectors.toList());
		}
		List<ApiProductVo> apiProductVos = remoteGoodsService
				.getSolitaireActivityProduct(apiSolitaireActivityVo.getProductType(), productIds);
		StringBuilder msg = new StringBuilder();
		String activityStatus = "已开始，大家速度采购吖~\n";
		if(ActivityStatusEnum.BE_GOING.getStatus() == apiSolitaireActivityVo.getStatus()){
			activityStatus = "即将开始，大家可以放下手头上的事准备血拼啦~\n";
		}else if(ActivityStatusEnum.OVER.getStatus() == apiSolitaireActivityVo.getStatus()) {
			activityStatus = "已结束，我们下次再见~\n";
		}
		if (CollectionUtil.isNotEmpty(apiProductVos)) {
			msg.append(DateUtil.format(apiSolitaireActivityVo.getStartTime(), "yyyy年MM月dd日 HH:mm")+ "-" + DateUtil.format(apiSolitaireActivityVo.getEndTime(), "yyyy年MM月dd日 HH:mm") + activityStatus);
			msg.append("参与活动商品\uD83D\uDC47\n");
			for (int i = 0; i < apiProductVos.size(); i++) {
				ApiProductVo apiProductVo = apiProductVos.get(i);
				BigDecimal lowPrice = apiProductVo.getSkuStocks().stream().map(ApiSkuStockVo::getPrice)
						.min(Comparator.naturalOrder()).get();
				msg.append(i + 1).append(".").append(apiProductVo.getName()).append("    ￥")
						.append(String.valueOf(lowPrice)).append("元\n");
			}
			msg.append("\uD83D\uDD0D参与戳这里\uD83D\uDC47:\n");
		}

		return Result.ok(msg);
	}

	/**
	 * 保存当前接龙活动接龙信息
	 *
	 * @param orderVo
	 * @return 接龙活动信息对象
	 */
	@Override
	public void saveCurrentActivityRecord(OrderVo orderVo) {
		log.info("-------------插入接龙活动记录开始-------------" + orderVo.toString());
		//先跟接龙活动id判断是否为接龙订单 是的话插入数据 不是直接pass
		if (orderVo.getSolitaireActivityId() != null) {
			//拼接接龙信息
			List<OrderItemVo> orderItemList = orderVo.getOrderItemList();
			if (CollectionUtil.isNotEmpty(orderItemList)) {
				orderItemList.stream().forEach(orderItem -> {
					SolitaireRecord solitaireRecord = new SolitaireRecord();
					solitaireRecord.setOrderId(String.valueOf(orderVo.getId()));
					solitaireRecord.setTenantId(orderVo.getTenantId());
					solitaireRecord.setShopId(orderVo.getShopId());
					solitaireRecord.setActivityId(String.valueOf(orderVo.getSolitaireActivityId()));
					solitaireRecord.setBuyerIcon(orderVo.getUserAvatarUrl());
					solitaireRecord.setBuyerName(orderVo.getUserName());
					solitaireRecord.setBuyerId(orderVo.getUserId());
					solitaireRecord.setProductName(orderItem.getProductName());
					solitaireRecord.setProductQuantity(orderItem.getProductQuantity());
					solitaireRecord.setPayAmount(orderVo.getPayAmount());
					int insert = apiSolitaireRecordMapper.insert(solitaireRecord);
					if (insert == 0) {
						throw new ServiceException("接龙信息插入失败！", SystemCode.DATA_ADD_FAILED.getCode());
					}
				});
			}
		}
		log.info("-------------插入接龙活动记录结束-------------");
	}

	/**
	 * 转发当前接龙活动接龙信息
	 *
	 * @param activityId
	 * @param path
	 * @return 接龙活动信息对象
	 */
	@Override
	public Result getCurrentActivityRecord(Long activityId, String path) {
		//拼接接龙信息
		List<SolitaireRecord> solitaireRecords = apiSolitaireRecordMapper
				.selectList(new QueryWrapper<SolitaireRecord>().eq("activity_id", activityId));
		//只发送10条记录，中间的记录用省略号代替
		//记录多于10条时，展示第一条和最近9条，中间数据省略
		// 排序
		List<SolitaireRecord> res;
		//Collections.sort(orderItemList);
		int index;
		if (solitaireRecords.size() > 10) {
			res = new ArrayList<>();
			res.add(solitaireRecords.get(0));
			List<SolitaireRecord> lastNine = solitaireRecords.stream().skip(solitaireRecords.size() - 9)
					.collect(Collectors.toList());
			res.addAll(lastNine);
			index = solitaireRecords.size() - 9;
		} else {
			res = solitaireRecords;
			index = 1;
		}
		StringBuilder msg = new StringBuilder();
		SolitaireActivity solitaireActivity = this.baseMapper.selectById(activityId);
		msg.append(solitaireActivity.getContent()).append("%0a");
		msg.append("\uD83C\uDF6D接龙开始:%0a");
		for (int i = 0; i < res.size(); i++) {
			SolitaireRecord solitaireRecord = res.get(i);
			if (i == 0) {
				msg.append(1).append(".").append(solitaireRecord.getBuyerName()).append(":");
			} else {
				index++;
				msg.append(index).append(".").append(solitaireRecord.getBuyerName()).append(":");
			}
			if (CollectionUtil.isNotEmpty(solitaireRecords)) {
				solitaireRecords.stream().forEach(orderItem -> {
					msg.append(orderItem.getProductName()).append("➕+").append(orderItem.getProductQuantity())
							.append("%0a");

				});
			}
			if (solitaireRecords.size() > 10 && i == 0) {
				msg.append("...%0a");
				msg.append("...%0a");
			}
		}
		msg.append("\uD83D\uDD0D参与戳这里\uD83D\uDC47:%0a").append(path);
		return Result.ok(msg);
	}

	/**
	 * 根据商品获取当前参与的最新一场接龙活动信息
	 *
	 * @param productId
	 * @return Result
	 */
	@Override
	public Result getActivityByProduct(Long productId) {
		//获取未开始与进行中的活动
		List<ApiSolitaireActivityVo> apiSolitaireActivityVos = this.baseMapper.queryReadyAndGoingActivity();
		if(CollectionUtil.isNotEmpty(apiSolitaireActivityVos)){
			Optional<ApiSolitaireActivityVo> optional = apiSolitaireActivityVos.stream().filter(apiSolitaireActivityVo -> {
				//全部商品参与的话直接返回 部分商品参与判断参与的商品是否包含该商品
				if(ActivityProductTypeEnum.ALL_JOIN.getStatus() == apiSolitaireActivityVo.getProductType()){
					return true;
				}
				if(ActivityProductTypeEnum.SOME_JOIN.getStatus() == apiSolitaireActivityVo.getProductType()){
					List<ApiSolitaireActivityProductVo> apiSolitaireActivityProductVos = apiSolitaireActivityVo.getApiSolitaireActivityProductVos();
					if(CollectionUtil.isNotEmpty(apiSolitaireActivityProductVos)){
						List<Long> productIds = apiSolitaireActivityProductVos.stream().map(val -> val.getProductId()).collect(Collectors.toList());
						if(CollectionUtil.isNotEmpty(productIds) && productIds.contains(productId)){
							return true;
						}
					}
				}
				return false;
			}).findAny();
			if(optional.isPresent()){
				return Result.ok(optional.get().getId());
			}else{
				return Result.failed( "商品暂未参与活动!");
			}
		}
		return Result.failed( "商品暂未参与活动!");
	}
}
